package com.example.q_kang.hhh.order;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.example.q_kang.hhh.R;
import com.example.q_kang.hhh.view.ViewPagerSlide;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.base.MVPFragment;
import com.sskj.base.adapter.BaseAdapter;
import com.sskj.base.adapter.MyFragmentPagerAdapter;
import com.sskj.base.adapter.ViewHolder;
import com.sskj.base.adapter.slimadapter.IViewInjector;
import com.sskj.base.adapter.slimadapter.SlimAdapter;
import com.sskj.base.adapter.slimadapter.SlimInjector;
import com.sskj.base.api.Constant;
import com.sskj.base.bean.OrderCommmonBean;
import com.sskj.base.bean.rxbus.MainChangeTabBean;
import com.sskj.base.bean.rxbus.RefreshOrderBean;
import com.sskj.base.mvchelper.ModelRx2DataSource;
import com.sskj.base.rxbus2.RxBus;
import com.sskj.base.rxbus2.Subscribe;
import com.sskj.base.rxbus2.ThreadMode;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.view.DividerLineItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;
import me.majiajie.pagerbottomtabstrip.PageNavigationView;

/**
 * @author Hey
 * Create at  2019/04/09
 */
public class OrderFragment extends MVPFragment<OrderPresenter> {
    @BindView(R.id.navigationView)
    PageNavigationView navigationView;
    @BindView(R.id.btn_buy)
    Button btnBuy;
    @BindView(R.id.btn_sell)
    Button btnSell;
    //图片
    @BindView(R.id.iv_match)
    ImageView ivMatch;
    @BindView(R.id.iv_remit)
    ImageView ivRemit;
    @BindView(R.id.iv_confirm)
    ImageView ivConfirm;
    @BindView(R.id.iv_complete)
    ImageView ivComplete;
    //文字
    @BindView(R.id.tv_match)
    TextView tvMatch;
    @BindView(R.id.tv_remit)
    TextView tvRemit;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_complete)
    TextView tvComplete;
    //小黄点数字
    @BindView(R.id.tv_circle_match)
    TextView tvCircleMatch;
    @BindView(R.id.tv_circle_remit)
    TextView tvCircleRemit;
    @BindView(R.id.tv_circle_confirm)
    TextView tvCircleConfirm;
    @BindView(R.id.tv_circle_complete)
    TextView tvCircleComplete;
    //小黄点显示
    @BindView(R.id.rl_circle_match)
    RelativeLayout rlCircleMatch;
    @BindView(R.id.rl_circle_remit)
    RelativeLayout rlCircleRemit;
    @BindView(R.id.rl_circle_confirm)
    RelativeLayout rlCircleConfirm;
    @BindView(R.id.rl_circle_complete)
    RelativeLayout rlCircleComplete;
    //下划线
    @BindView(R.id.view_match)
    View viewMatch;
    @BindView(R.id.view_remit)
    View viewRemit;
    @BindView(R.id.view_confirm)
    View viewConfirm;
    @BindView(R.id.view_complete)
    View viewComplete;
    //点击切换
    @BindView(R.id.rl_match)
    RelativeLayout rlMatch;
    @BindView(R.id.rl_remit)
    RelativeLayout rlRemit;
    @BindView(R.id.rl_confirm)
    RelativeLayout rlConfirm;
    @BindView(R.id.rl_complete)
    RelativeLayout rlComplete;
    @BindView(R.id.buyViewPager)
    ViewPagerSlide buyViewPager;
    @BindView(R.id.sellViewPager)
    ViewPagerSlide sellViewPager;

    private int type = Constant.BUY;
    private ArrayList<Fragment> buyFragments = new ArrayList<>();
    private ArrayList<Fragment> sellFragments = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.fragment_order;
    }

    @Override
    public OrderPresenter getPresenter() {
        return new OrderPresenter();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            mPresenter.getOrderCommonData();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getOrderCommonData();
    }

    @Override
    public void initView() {
        ClickUtil.click(200, btnBuy, view -> {
            mPresenter.getOrderCommonData();
            titleChange(0);
        });
        ClickUtil.click(200, btnSell, view -> {
            mPresenter.getOrderCommonData();
            titleChange(1);
        });
        ClickUtil.click(200, rlMatch, view -> {
            mPresenter.getOrderCommonData();
            tabChange(0);
        });
        ClickUtil.click(200, rlRemit, view -> {
            mPresenter.getOrderCommonData();
            tabChange(1);
        });
        ClickUtil.click(200, rlConfirm, view -> {
            mPresenter.getOrderCommonData();
            tabChange(2);
        });
        ClickUtil.click(200, rlComplete, view -> {
            mPresenter.getOrderCommonData();
            tabChange(3);
        });

        buyFragments.add(BuyWaitPipeiFragment.newInstance());
        buyFragments.add(BuyWaitDaKuanFragment.newInstance());
        buyFragments.add(BuyWaitConfirmFragment.newInstance());
        buyFragments.add(BuyCompleteFragment.newInstance());
        sellFragments.add(SellWaitPipeiFragment.newInstance());
        sellFragments.add(SellWaitDaKuanFragment.newInstance());
        sellFragments.add(SellWaitConfirmFragment.newInstance());
        sellFragments.add(SellCompleteFragment.newInstance());
        buyViewPager.setSlide(false);
        buyViewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), buyFragments));
        buyViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                tabChange(position);
            }
        });
        sellViewPager.setSlide(false);
        sellViewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), sellFragments));
        sellViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                tabChange(position);
            }
        });
    }

    @Override
    public void initData() {
        mPresenter.getOrderCommonData();

    }

    public static OrderFragment newInstance() {
        OrderFragment fragment = new OrderFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    private void titleChange(int position) {
        RxBus.getDefault().post(new RefreshOrderBean());
        switch (position) {
            case 0:
                btnBuy.setBackgroundResource(R.drawable.bg_solid_blue_left_corner);
                btnBuy.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                btnSell.setBackgroundResource(R.drawable.bg_stroke_blue_right_corner);
                btnSell.setTextColor(ContextCompat.getColor(getActivity(), R.color.theme));
                buyViewPager.setVisibility(View.VISIBLE);
                sellViewPager.setVisibility(View.GONE);
                type = Constant.BUY;
                break;
            case 1:
                btnBuy.setBackgroundResource(R.drawable.bg_stroke_blue_left_corner);
                btnBuy.setTextColor(ContextCompat.getColor(getActivity(), R.color.theme));
                btnSell.setBackgroundResource(R.drawable.bg_solid_blue_right_corner);
                btnSell.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                buyViewPager.setVisibility(View.GONE);
                sellViewPager.setVisibility(View.VISIBLE);
                type = Constant.SELL;
                break;
        }
        tabChange(0);
    }

    private void tabChange(int position) {
        RxBus.getDefault().post(new RefreshOrderBean());
        viewMatch.setVisibility(View.INVISIBLE);
        viewRemit.setVisibility(View.INVISIBLE);
        viewConfirm.setVisibility(View.INVISIBLE);
        viewComplete.setVisibility(View.INVISIBLE);
        tvMatch.setTextColor(ContextCompat.getColor(getActivity(), R.color.color_CCC));
        tvRemit.setTextColor(ContextCompat.getColor(getActivity(), R.color.color_CCC));
        tvConfirm.setTextColor(ContextCompat.getColor(getActivity(), R.color.color_CCC));
        tvComplete.setTextColor(ContextCompat.getColor(getActivity(), R.color.color_CCC));
        ivMatch.setImageResource(R.mipmap.icon_wait_match_no);
        ivRemit.setImageResource(R.mipmap.icon_wait_remit_no);
        ivConfirm.setImageResource(R.mipmap.icon_wait_confirm_no);
        ivComplete.setImageResource(R.mipmap.icon_complete_no);
        switch (position) {
            case 0:
                viewMatch.setVisibility(View.VISIBLE);
                tvMatch.setTextColor(ContextCompat.getColor(getActivity(), R.color.theme));
                ivMatch.setImageResource(R.mipmap.icon_wait_match_yes);
                break;
            case 1:
                viewRemit.setVisibility(View.VISIBLE);
                tvRemit.setTextColor(ContextCompat.getColor(getActivity(), R.color.theme));
                ivRemit.setImageResource(R.mipmap.icon_wait_remit_yes);
                break;
            case 2:
                viewConfirm.setVisibility(View.VISIBLE);
                tvConfirm.setTextColor(ContextCompat.getColor(getActivity(), R.color.theme));
                ivConfirm.setImageResource(R.mipmap.icon_wait_confirm_yes);
                break;
            case 3:
                viewComplete.setVisibility(View.VISIBLE);
                tvComplete.setTextColor(ContextCompat.getColor(getActivity(), R.color.theme));
                ivComplete.setImageResource(R.mipmap.icon_complete_yes);
                break;
            default:
                break;
        }
        switch (type) {
            case Constant.BUY:
                if (buyViewPager.getCurrentItem() != position) {
                    buyViewPager.setCurrentItem(position, true);
                }
                break;
            case Constant.SELL:
                if (sellViewPager.getCurrentItem() != position) {
                    sellViewPager.setCurrentItem(position, true);
                }
                break;
        }
    }

    public void getOrderDataSucess(OrderCommmonBean orderCommmonBean) {
        if (orderCommmonBean == null) {
            return;
        }
        if (type == Constant.BUY) {
            tvCircleMatch.setText(orderCommmonBean.getPdTgbzOne());
            tvCircleRemit.setText(orderCommmonBean.getPdTgbzTwo());
            tvCircleConfirm.setText(orderCommmonBean.getPdTgbzThree());
            tvCircleComplete.setText(orderCommmonBean.getPdTgbzFour());
        }
        if (type == Constant.SELL) {
            tvCircleMatch.setText(orderCommmonBean.getPdJsbzOne());
            tvCircleRemit.setText(orderCommmonBean.getPdJsbzTwo());
            tvCircleConfirm.setText(orderCommmonBean.getPdJsbzThree());
            tvCircleComplete.setText(orderCommmonBean.getPdJsbzFour());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeTab(MainChangeTabBean mainTabChange) {
        titleChange(mainTabChange.getType());
    }
}
